﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for clsEmail
/// </summary>
public class clsEmail
{
    static string strClassName = "clsEmail";

	public clsEmail()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// Gửi Email quà tặng
    /// </summary>
    /// <param name="strHoTen">Họ tên người nhận</param>
    /// <param name="strEmail">Email</param>
    /// <returns></returns>
    public static bool Send_Mail_Qua_Tang(string strHoTen, string strEmail)
    {
        //Gửi mail
        string strNoiDung = OsPortal.oFileHelper.ReadFile(basConst.LinkFormMailNhanQuaTang);
        strNoiDung = strNoiDung.Replace("HO_TEN_USER", strHoTen);

        return SendMailFromNghiKhac(strEmail, "QUÀ TẶNG TỪ NGHIKHAC.COM", strNoiDung);
    }


    /// <summary>
    /// Gửi email từ xomtrosv.com
    /// </summary>
    /// <param name="strToMail">Địa chỉ email nhận thư</param>
    /// <param name="strNoiDung"></param>
    /// <returns></returns>
    public static bool SendMailFromNghiKhac(string strToMail, string strTieuDe, string strNoiDung)
    {
        string strUserName = System.Configuration.ConfigurationManager.AppSettings["USERNAME_EMAIL"].ToString();
        string strPassWord = System.Configuration.ConfigurationManager.AppSettings["PASSWORD_EMAIL"].ToString();
        string strSmtp = System.Configuration.ConfigurationManager.AppSettings["SMTP_EMAIL"].ToString();
        return sendMail(strUserName, strPassWord, "NghiKhac.Com", "support@nghikhac.com", strToMail, strTieuDe, strNoiDung, strSmtp);
    }

    /// <summary>
    /// Gửi Email
    /// </summary>
    /// <param name="strUserName">UserName</param>
    /// <param name="strPassWord">Password</param>
    /// <param name="strName">Họ tên người gửi</param>
    /// <param name="strFrom">Địa chỉ mail của người gửi</param>
    /// <param name="strTo">Địa chỉ mail của người nhận</param>
    /// <param name="strSubject">Chủ Đề</param>
    /// <param name="strContent">Nội dung</param>
    /// <param name="strTypeMail">sử dụng "gmail - smtp.gmail.com" hay "yahoo - smtp.mail.yahoo.com"</param>
    /// <returns></returns>
    public static bool sendMail(string strUserName,
                        string strPassWord,
                        string strName,
                        string strFrom,
                        string strTo,
                        string strSubject,
                        string strContent,
                        string strTypeMail)
    {
        MailMessage mailMsg;
        SmtpClient client;
        NetworkCredential info;
        try
        {
            mailMsg = new MailMessage();
            mailMsg.From = new MailAddress(strFrom, strName);
            mailMsg.To.Add(strTo);
            mailMsg.Subject = strSubject;
            mailMsg.IsBodyHtml = true;
            mailMsg.Body = strContent;
            mailMsg.Priority = MailPriority.Normal;
            client = new SmtpClient(strTypeMail, 587);
            info = new NetworkCredential(strUserName, strPassWord);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = info;
            client.EnableSsl = true;
            client.Send(mailMsg);
            return true;
        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "sendMail", ex.ToString());
            return false;
        }
    }

}